<?php

namespace common\models\Fenhui;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\web\IdentityInterface;
use common\models\Fenhuigrade\Fenhuigrade;
use yii\helpers\ArrayHelper;
/**
 * This is the model class for table "fenhui".
 *
 * @property integer $id
 * @property integer $userid
 * @property string $grade
 * @property string $qiyemingcheng
 * @property string $farendaibiao
 * @property string $qiyedizhi
 * @property integer $youbian
 * @property string $farentel
 * @property string $farenmobile
 * @property string $farenfax
 * @property string $contact
 * @property string $tel
 * @property string $mobile
 * @property string $fax
 * @property string $qiyewangzhi
 * @property string $email
 * @property integer $qiyerenshu
 * @property string $shigongzizhi
 * @property string $certificatenumber
 * @property string $zhucezijin
 * @property string $konggugudong
 * @property string $qiyepinpai
 * @property integer $jingjileixing
 * @property integer $qiyexingzhi
 * @property string $product
 * @property string $qiyejianjie
 * @property integer $flag
 */
class Fenhui extends \yii\db\ActiveRecord
{
    public $password;
    public $repassword;
    private $_statusLabel;
    private $_gradeLabel;
    private $_isopenLabel;
    const STATUS_qiyong= 10;  //分会是否启用
    const STATUS_DELETED = 2; //再审核
    const STATUS_ACTIVE = 3; //待审核
    const STATUS_SUCCESS = 4; //已审核
    const STATUS_tongguo= 1;  //审核通过
    const STATUS_butongguo= 0;  //审核未通过

    

    const IS_PAID = 1;
    const NO_PAID = 0;
    public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'fenhui';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['userid', 'youbian', 'qiyerenshu', 'jingjileixing', 'qiyexingzhi', 'flag'], 'integer'],
            [['tel', 'mobile', 'fax', 'email', 'flag'], 'required'],
            [['grade', 'qiyemingcheng', 'farendaibiao', 'qiyedizhi', 'farenfax', 'contact', 'tel', 'fax', 'qiyewangzhi', 'email', 'shigongzizhi', 'certificatenumber', 'zhucezijin', 'konggugudong', 'qiyepinpai'], 'string', 'max' => 255],
            [['farentel', 'farenmobile', 'mobile'], 'string', 'max' => 32],
            [['product', 'qiyejianjie'], 'string', 'max' => 1020],
            [['contact'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'userid' => Yii::t('app', '会员id'),
            'name' => Yii::t('app', '姓名'),
            'zhiwu' => Yii::t('app', '职务'),
            'province' => Yii::t('app', '省'),
            'city' => Yii::t('app', '市'),
            'towm' => Yii::t('app', '县'),
            'grade' => Yii::t('app', '所属分会'),
            'shangbiaotu' => Yii::t('app', '商标图'),
            'qiyemingcheng' => Yii::t('app', '企业名称'),
            'farendaibiao' => Yii::t('app', '法人代表'),
            'qiyedizhi' => Yii::t('app', '企业地址'),
            'youbian' => Yii::t('app', '邮编'),
            'farentel' => Yii::t('app', '法人电话'),
            'farenmobile' => Yii::t('app', '法人手机'),
            'farenfax' => Yii::t('app', '法人传真'),
            'contact' => Yii::t('app', '分会联系人'),
            'tel' => Yii::t('app', '电话'),
            'mobile' => Yii::t('app', '手机'),
            'fax' => Yii::t('app', '传真'),
            'qiyewangzhi' => Yii::t('app', '企业网址'),
            'email' => Yii::t('app', '联系人邮箱'),
            'qiyerenshu' => Yii::t('app', '企业人数'),
            'shigongzizhi' => Yii::t('app', '施工资质'),
            'certificatenumber' => Yii::t('app', '资质证书编号'),
            'zhucezijin' => Yii::t('app', '注册资金'),
            'konggugudong' => Yii::t('app', '控股股东'),
            'qiyepinpai' => Yii::t('app', '企业品牌或商标'),
            'jingjileixing' => Yii::t('app', '企业经济类型'),
            'qiyexingzhi' => Yii::t('app', '企业性质'),
            'product' => Yii::t('app', '产品信息'),
            'qiyejianjie' => Yii::t('app', '企业简介'),
            'flag' => Yii::t('app', '审核是否通过'),
            'created_at' => Yii::t('app', '创建时间'),
            'updated_at' => Yii::t('app', '更新时间'),
            'sbwenzi' => Yii::t('app', '代理商标文字'),
            'yyzhizao' => Yii::t('app', '营业执照号'),
            'shuiwudj' => Yii::t('app', '税务登记号'),
            'chenglidate' => Yii::t('app', '企业成立日期'),
            'zuzhijgdm' => Yii::t('app', '组织机构代码'),
            'danweixz' => Yii::t('app', '单位性质'),
            'danweizjlx' => Yii::t('app', '单位证件类型'),
            'zhubanzjhm' => Yii::t('app', '主办单位证件号码'),
            'zhucedizhi' => Yii::t('app', '注册地址'),
            'jyfanwei' => Yii::t('app', '经营范围'),
            'farenjianjie' => Yii::t('app', '法人代表简介'),
            'yyzhizhaoimg' => Yii::t('app', '营业执照'),
            'shuiwudenji' => Yii::t('app', '税务登记证'),
            'zhuzhijgdmz' => Yii::t('app', '组织机构代码证'),
            'jymoshi' => Yii::t('app', '经营模式'),
            'dailipinpai' => Yii::t('app', '代理品牌'),
            'bennum' => Yii::t('app', '本科及以上人数'),
            'waixiao' => Yii::t('app', '外销比例'),
            'chanpinzl' => Yii::t('app', '企业产品种类'),
            'toubiao' => Yii::t('app', '是否参加过政府采购投标'),
        ];
    }
    public static function getArrayGrade(){
        return ArrayHelper::map(Fenhuigrade::find()->andWhere(['status'=>Fenhui::STATUS_qiyong])->all(), 'id', 'name');
    }
    public function getGradeLabel()
    {

        if ($this->_gradeLabel === null) {
            $grade = self::getArrayGrade();
            $this->_gradeLabel =  array_key_exists($this->grade,self::getArrayGrade()) ? $grade[$this->grade] : '无';
        }
        return $this->_gradeLabel;
    }  
     public function getStatusLabel()
    {
        if ($this->_statusLabel === null) {
            $statuses = self::getArrayStatus();
            $this->_statusLabel = array_key_exists($this->flag,self::getArrayStatus()) ? $statuses[$this->flag] : '无';
        }
        return $this->_statusLabel;
    }
    /**
     * @inheritdoc
     */
    /**
     * @inheritdoc
     */
    public static function getArrayStatus()
    {
        return [
            self::STATUS_SUCCESS => Yii::t('app', '已审核'),
            self::STATUS_ACTIVE => Yii::t('app', '待审核'),
            self::STATUS_DELETED => Yii::t('app', '再审核'),
            self::STATUS_tongguo => Yii::t('app', '审核通过'),
            self::STATUS_butongguo => Yii::t('app', '审核未通过'),

        ];
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id]);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }

    /**
     * Finds user by username
     *
     * @param string $username
     * @return static|null
     */
    /*public static function findBycontact($username)
    {
        return static::findOne(['contact' => $username]);
    }*/

    /**
     * Finds user by password reset token
     *
     * @param string $token password reset token
     * @return static|null
     */
    public static function findByPasswordResetToken($token)
    {
        if (!static::isPasswordResetTokenValid($token)) {
            return null;
        }

        return static::findOne([
            'password_reset_token' => $token
        ]);
    }
    /**
     * Finds out if password reset token is valid
     *
     * @param string $token password reset token
     * @return boolean
     */
    public static function isPasswordResetTokenValid($token)
    {
        if (empty($token)) {
            return false;
        }
        $expire = Yii::$app->params['user.passwordResetTokenExpire'];
        $parts = explode('_', $token);
        $timestamp = (int) end($parts);
        return $timestamp + $expire >= time();
    }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password)
    {
        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }

    /**
     * Generates password hash from password and sets it to the model
     *
     * @param string $password
     */
    public function setPassword($password)
    {
        $this->password_hash = Yii::$app->security->generatePasswordHash($password);
    }

    /**
     * Generates "remember me" authentication key
     */
    public function generateAuthKey()
    {
        $this->auth_key = Yii::$app->security->generateRandomString();
    }

    /**
     * Generates new password reset token
     */
    public function generatePasswordResetToken()
    {
        $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
    }
    /**
     * Removes password reset token
     */
    public function removePasswordResetToken()
    {
        $this->password_reset_token = null;
    }  
}
